package com.onenine.app.utils;


import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.onenine.common.util.ApUtils;

import java.io.IOException;

/**
 * 音频(录音)播放
 *
 * @author zhuhs
 * created at 2016/9/23 13:51
 */
public class SoundPlayer {

    //流媒体播放实例
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private OnSoundListener mSoundListener;

    public interface OnSoundListener {
        //播放开始
        void onStart(MediaPlayer mp);

        //播放结束
        void onStop(MediaPlayer mp);

        //播放错误
        void onError(MediaPlayer mp);
    }

    public SoundPlayer() {

    }

    public SoundPlayer(OnSoundListener soundListener) {
        setSoundListener(soundListener);
    }

    /**
     * 设置音频播放模式
     *
     * @param state
     */
    public static void setAudioPlayByEarPhone(int state) {
        AudioManager audioManager = (AudioManager) ApUtils.getApp()
                .getSystemService(Context.AUDIO_SERVICE);
        // 外放模式
        if (state == 0) {
            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.setSpeakerphoneOn(true);
            // 耳机模式
        } else if (state == 1) {
            audioManager.setSpeakerphoneOn(false);
            // 听筒模式
        } else {
            audioManager.setSpeakerphoneOn(false);
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, currVolume,
                    AudioManager.STREAM_VOICE_CALL);
        }
    }

    /**
     * 播放网络音频(录音)
     *
     * @param uriOrFilePath
     */
    public void start(String uriOrFilePath) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(uriOrFilePath);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                    if (mSoundListener != null) {
                        mSoundListener.onStart(mp);
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止播放音频(录音)
     */
    public void stop() {
        try {
            mediaPlayer.stop();
            mediaPlayer.release();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 暂停音频
     */
    public void pause() {
        try {
            mediaPlayer.pause();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复音频
     */
    public void restart() {
        try {
            mediaPlayer.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放音频资源
     */
    public void release() {
        try {
            mediaPlayer.release();
            mediaPlayer = null;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置播放监听
     *
     * @param soundListener
     */
    public void setSoundListener(OnSoundListener soundListener) {
        mSoundListener = soundListener;
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mSoundListener.onStop(mp);
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mSoundListener.onError(mp);
                return false;
            }
        });
    }

    /**
     * 是否正在播放
     *
     * @return
     */
    public boolean isPlaying() {
        try {
            return mediaPlayer != null && mediaPlayer.isPlaying();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
